/*******************************************************************************
 *  PasswordControl class
 *  desc: Wrapper for InputElement
 *******************************************************************************

// example options object
var options = {
    id: 'id',
    name: 'name',
    checkForStrong: false
    placeholder: 'placeholder',   
    classes: 'classes',
    value: 'value',
    title: 'title'
}

*/

/**
 *  Represent password control model
 */
var PasswordControl = function(options) {
    
    $.extend(true, options, {
        type: 'password',
        rules: {
            isPasswordStrong: true
        }
    })
    
    // this class should extend an Element class
    $.extend(true, this, new InputControl(options));
    
    // ------------------------------------------------------- settings
    this.defaults = {
    };
    
    var settings = this.prepareSettings(options);
    
    // ------------------------------------------------------- element creation
    
    this.holder = new Element($('<div class="btn-group btn-group-vertical">').css('height', '46px'));

    this.scoreBar = new Element($('<div>'))
    this.scoreBar.setId(this.getId()+'score-bar').addClass('progress').css({
        'margin-top': '-4px',
        'background-color': 'white',
        'background-image': 'none',
        'box-shadow': 'none'
    });

    this.scoreBar.bar = new Element($('<div class="bar" style="width: 0%">'));
    this.scoreBar.append(this.scoreBar.bar);

    this.holder.append(this.getElementByIndex(0)).append(this.scoreBar);

    this.fieldsHolder.append(this.holder);

    var scoreBar = this.scoreBar;
    
    $.validator.addMethod("isPasswordStrong", function(value){
        var result = chkPass(value);
        var score = result.score;
        var complexity = result.complexity;

        // prepares message
        var text = ''
        if(complexity!='Very Weak' && complexity!='Weak' && complexity!='Too Short'){
            text = $.validator.format('{0} ({1}%)', complexity, score);
        }

        // prepares color
        var type = 'danger';
        if (score >= 0 && score < 20) {type = "danger";}
        else if (score >= 20 && score < 40) {type = "warning";}
        else if (score >= 40 && score < 60) {type = "warning";}
        else if (score >= 60 && score < 80) {type = "success";}
        else if (score >= 80 && score <= 100) {type = "success";}

        // sets all prepapred values
        scoreBar.bar.setText(text);
        scoreBar.bar.css('width', score+'%');
        scoreBar.removeClass('progress-danger progress-warning progress-success').addClass('progress-'+type);

        return score>=60; // strong is required
    }, 'Please, type strong password');
    
    console.log('InputControl was created with id = '+this.getId());
}